package com.ropeok.service.serviceimpl;

import com.ropeok.Exception.ServiceException;
import com.ropeok.constant.ResultConstant;
import com.ropeok.entity.*;
import com.ropeok.mapper.*;
import com.ropeok.service.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@Service
public class ServiceImpl implements service {

    @Autowired
    UserMapper usermapper;
    @Autowired
    PermissionMapper permissionmapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserAndRoleMapper userAndRoleMapper;

    @Autowired
    role_permissionMapper role_permissionMapper;

    /**
     * 将所有用户所有信息查找出来，包括每个用户扮演的角色，拥有的权限
     * @return
     */
    @Override
    public List<User> list() {

            List<User> userList = usermapper.FindAll();
//            int i=1/0;//测试事务
            for (User user : userList) {
                user.setLrole(roleMapper.findByUserid(user.getUserid()));
                List<Role> lrole = user.getLrole();
                for (Role role : lrole)
                    user.setLpermissions(permissionmapper.findByRoleId(role.getRoleid()));
            }
//            int i=1/0;
            return userList;
    }




    /**
     * 删除一个用户，同时也要把用户角色关系表中的该用户删除，加事务，两个必须同时被删除
     * @param id
     */
    @Override
    @Transactional
    public void DelUser(Integer id) {
        List<Integer> AllUserIds = usermapper.FindUserId();
        if(!AllUserIds.contains(id)){
            throw new ServiceException(ResultConstant.ID_NOT_EXIST,"用户id不存在");
        }
        usermapper.DelUser(id);
        userAndRoleMapper.DelURUser(id);
    }

    /**
     * 新增用户
     * @param user
     */
    @Override
    public void SaveUser(User user) {
        List<Integer> AllUserId = usermapper.FindUserId();
        if (AllUserId.contains(user.getUserid())){
            throw new ServiceException(ResultConstant.ID_IS_EXIST,"用户已存在");
        }
        usermapper.Save(user);
    }

    @Override
    public void SaveRole(Role role) {
        List<Integer> AllRoleId = roleMapper.FindRoleId();
        if (AllRoleId.contains(role.getRoleid())) {
            throw new ServiceException(ResultConstant.ID_IS_EXIST,"角色已存在");
        }
        roleMapper.Save(role);
    }

    @Override
    public void SavePermission(permissions permissions) {

        List<Integer> AllPermissionId = permissionmapper.FindPermissionId();

            if (AllPermissionId.contains(permissions.getPerid())){
                throw new ServiceException(ResultConstant.ID_IS_EXIST,"权限已存在");
            }
        permissionmapper.Save(permissions);
    }

    @Override
    public void SaveRoleAndPermission(RoleAndPermission roleAndPermission) {

        List<RoleAndPermission> roleAndPermissions = role_permissionMapper.FindAllRoleAndPermission();
            if (roleAndPermissions.contains(roleAndPermission)){
                throw new ServiceException(ResultConstant.RELATION_IS_EXIST,"关系已存在");
            }
        role_permissionMapper.SaveRp(roleAndPermission);
    }

    @Override
    public void SaveUserAndRole(UserAndRole userAndRole) {

        List<UserAndRole> userAndRoles = userAndRoleMapper.FindAllUserAndRole();
        if (userAndRoles.contains(userAndRole)){
            throw new ServiceException(ResultConstant.RELATION_IS_EXIST,"关系已存在");
        }
        userAndRoleMapper.saveUR(userAndRole);
    }




    /**
     * 当一个角色信息被删除后，他的角色权限中存的信息也一并删除
     * @param id
     */
    @Override
    @Transactional
    public void DelRole(Integer id){
        List<Integer> AllRoleIds = roleMapper.FindRoleId();
        if(!AllRoleIds.contains(id)){
            throw new ServiceException(ResultConstant.ID_NOT_EXIST,"角色id不存在");
        }
        roleMapper.DelRole(id);
        role_permissionMapper.DelRpRole(id);
    }

    /**
     * 当一个权限信息被删除后，他的角色权限中存的信息也一并删除
     * @param id
     */
    @Override
    @Transactional
    public void DelPermission(Integer id) {
        List<Integer> AllPermissionIds = permissionmapper.FindPermissionId();
        if(!AllPermissionIds.contains(id)){
            throw new ServiceException(ResultConstant.ID_NOT_EXIST,"权限id不存在");
        }
        permissionmapper.DelPermissions(id);
        role_permissionMapper.DelRpPermisssion(id);
    }

    @Override
    public void DelUserAndRoleByUser(Integer id) {
        List<Integer> AllUserId = usermapper.FindUserId();
        if (!AllUserId.contains(id)){
            throw new ServiceException(ResultConstant.ID_NOT_EXIST,"用户id不存在");
        }
        userAndRoleMapper.DelURUser(id);
    }

    @Override
    public void DelUserAndRoleByRole(Integer id) {
        List<Integer> AllRoleId = roleMapper.FindRoleId();
        if (!AllRoleId.contains(id)){
            if (!AllRoleId.contains(id)){
                throw new ServiceException(ResultConstant.ID_NOT_EXIST,"角色id不存在");
            }
        }
        userAndRoleMapper.DelURRole(id);
    }

    @Override
    public void DelRoleAndPermissionByRole(Integer id) {
        List<Integer> AllRoleId = roleMapper.FindRoleId();
        if (!AllRoleId.contains(id)){
            if (!AllRoleId.contains(id)){
                throw new ServiceException(ResultConstant.ID_NOT_EXIST,"角色id不存在");
            }
        }
        role_permissionMapper.DelRpRole(id);
    }

    @Override
    public void DelRoleAndPermissionByPermission(Integer id) {

        List<Integer> AllPermissionIds = permissionmapper.FindPermissionId();
        if (!AllPermissionIds.contains(id)){
            if (!AllPermissionIds.contains(id)){
                throw new ServiceException(ResultConstant.ID_NOT_EXIST,"权限id不存在");
            }
        }
        role_permissionMapper.DelRpPermisssion(id);
    }


}
